package com.mine.wechat.controller;

import com.mine.wechat.entity.ResObject;
import com.mine.wechat.entity.WeCont;
import com.mine.wechat.entity.WeDict;
import com.mine.wechat.service.WeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class WeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WeService weService;

    @RequestMapping("/wecont")
    public String getWeCont(Model model) {
        List<WeCont> list = weService.findAllWeCont();
        logger.info("==>"+list.toString());
        model.addAttribute("weConts",list);
        return "wecont";
    }

    @RequestMapping(value = "/wecont/add", method = RequestMethod.POST)
    public void addWeCont(WeCont weCont, HttpServletResponse response) throws IOException {
        logger.info("add=weCont=>"+weCont.toString());
        weService.insertWeCont(weCont);
        response.sendRedirect("/wecont");
    }

    @ResponseBody
    @RequestMapping(value = "/wecont/update", method = RequestMethod.POST)
    public ResObject updateWeCont(WeCont weCont) {
        logger.info("update=weCont=>"+weCont.toString());
        weService.updateWeCont(weCont);
        return new ResObject();
    }

    @RequestMapping("/wecont/{id}/update")
    public String toUpdateWeCont(@PathVariable Long id, Model model) {
        logger.info("to=update=weCont=>"+id);
        WeCont weCont = weService.findWeContById(id);
        model.addAttribute("weCont",weCont);
        return "addwecont";
    }

    @RequestMapping("/wecont/to/add")
    public String toAddWeCont(Model model) {

        WeCont weCont = new WeCont();
        weCont.setDate(new Date());
        model.addAttribute("weCont", weCont);

        return "addwecont";
    }

    @RequestMapping("/wecont/{id}/delete")
    public void deleteWeCont(@PathVariable Long id, HttpServletResponse response) throws IOException {
        logger.info("delete=weCont=>"+id);
        weService.deleteWeCont(id);
        response.sendRedirect("/wecont");
    }

    @RequestMapping("/wedict")
    public String getWeDict(Model model) {
        List<WeDict> list = weService.findAllWeDict();
        logger.info("==>"+list.toString());
        model.addAttribute("weDicts",list);
        return "wedict";
    }

    @RequestMapping(value = "/wedict/add", method = RequestMethod.POST)
    public void addWeDict(WeDict weDict, HttpServletResponse response) throws IOException {
        logger.info("add=weDict==>"+weDict);
        weService.insertWeDict(weDict);
        response.sendRedirect("/wedict");
    }

    @ResponseBody
    @RequestMapping(value = "/wedict/update", method = RequestMethod.POST)
    public ResObject updateWeDict(WeDict weDict) {
        logger.info("update=weDict==>"+weDict);
        weService.updateWeDict(weDict);
        return new ResObject();
    }

    @RequestMapping("/wedict/{id}/delete")
    public void addWeDict(@PathVariable Long id, HttpServletResponse response) throws IOException {
        logger.info("delete=weDict==>"+id);
        weService.deleteWeDict(id);
        response.sendRedirect("/wedict");
    }

    @RequestMapping("/wedict/{id}/update")
    public String toUpdateWeDict(@PathVariable Long id, Model model) {
        logger.info("to=update=weDict=>"+id);
        WeDict weDict = weService.findWeDictById(id);
        model.addAttribute("weDict",weDict);
        return "addwedict";
    }

    @RequestMapping("/wedict/to/add")
    public String toAddWeDict(Model model) {

        model.addAttribute("weDict", new WeDict());

        return "addwedict";
    }

}
